#include "sound_app.h"
#include "event_app.h"
#include "finger_board.h"
#include "protocol_api.h"
#include "temporary_file.h"
#include "cardholder_api.h"
#include "device_parameter.h"

#if (_SAFETY_MODULE_TYPE_ == _FINGER_SAFETY_MODULE_) && (_SAFETY_VERIFYFUNCTION_ENABLE_)

void FingerPreparationsBeforeLocalRegister(uint8_t WaitDelay)
{
  EventSetLocalRegisterKeyStep(LOCAL_REGISTER_PRESS_FINGER);
  EventUpdateLocalRegisterKeyResult(LOCAL_REGISTER_RESULT_BUSY);
}

void FingerStartLocalRegister(void)
{
  PlayStartPrintingAudio();
  EventSetLocalRegisterKeyStep(LOCAL_REGISTER_PRESS_FINGER);
}

void FingerUpdateRegisterPrintfAgain(void)
{
  PlayReadmissionPrintingAudio();
  EventSetLocalRegisterKeyStep(LOCAL_REGISTER_REPEAT_FINGER_1);
}

bool FingerGetDeviceActivationStatus(void)
{
  uint8_t WorkMode = GetDeviceInitStatus() & (~FIRMWARE_UPGRADE);
  if (WorkMode == NORMAL_MODE)
    return true;

  return false;
}

void FingerReloadLocalRegisterTimer(void)
{
}

uint8_t FingerGetSearchTimeout(void)
{
  return GetSearchTimeout();
}

void FingerReadEigenvalueFileInformation(uint32_t *pFileSizeOut)
{
  ProtocolReadTheEigenvalueFileInformation(pFileSizeOut, NULL);
}

bool FingerWriteEigenvalueFileInformation(uint32_t FileSize)
{
  return false;
}

void FingerReadFeatureValueFilesForMemory(uint32_t ReadOffset, uint8_t *pDataOut, uint16_t DateSize)
{
  ReadTheTemporaryFile(ReadOffset, pDataOut, DateSize);
}

bool FingerWriteFeatureValueFilesToMemory(uint32_t WriteOffset, uint8_t *pDataIn, uint16_t DateSize)
{
  return WriteToTemporaryFile(WriteOffset, pDataIn, DateSize);
}

bool FingerDeleteDuplicateUsers(uint16_t UserId)
{
  uint8_t DeleteUserName[USER_NAME_LENGTH] = {0}, DeleteKeyValue[USER_AUTHENTICATION_KEY_LENGTH] = {0};

  memset(DeleteKeyValue, 0, USER_AUTHENTICATION_KEY_LENGTH);
  memset(DeleteUserName, PROTOCOL_DUMMY_CODING, USER_NAME_LENGTH);

  DeleteKeyValue[0] = (uint8_t)((UserId & 0xFF00) >> 8);
  DeleteKeyValue[1] = (uint8_t)(UserId & 0x00FF);

  if (DeleteCardholders(FINGER_PRINTF_AUTHENTICATION, DeleteUserName, DeleteKeyValue) != DELETED_SUCCESSFULLY)
  {
    PRINT("[Finger]: failed to delete target user.\r\n");
    return false;
  }

  return true;
}

#endif
